<?php

namespace app\tenant\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class User extends Controller
{
    public function vipalllist()
    {
        // ->where(['store_id' => $storeId])->lists()
        $tenantid=trim(input('param.tenantid'));
        $this->db = new SysDb();

        $count = $this->db->table('tenant_vip_level')->where(['tenant_id' =>  $tenantid])->lists();

        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $count]));
    }
    
    public function userlist()
    {
        $parentId=trim(input('param.parentId'));
        $phone = trim(input('param.phone'));
        $id=trim(input('param.id'));
        $status=trim(input('param.status'));
        $levelId=trim(input('param.levelId'));
        // ->where(['store_id' => $storeId])->lists()
        $tenantid=trim(input('param.tenantid'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $where=array(
            "phone" => $phone,
            "parent_id" =>$parentId,
            "status"=>$status,
            "id"=>$id,
            "level_id"=>$levelId
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $list = DB::name("bus_info")->where(['tenant_id' =>  $tenantid])->where($where)->order("create_time desc")->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_info')->where($where)->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    
    public function userupdatelevel()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $levelId = trim(input('param.levelId'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
       
        $bus['level_id']=$levelId;
        $buses=$this->db->table('bus_info')->where(['id' => $id])->update($bus);
        if($buses){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
       
    }

    public function addflow()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.busId'));
        $amount = trim(input('param.amount'));
        $dealDesc = trim(input('param.dealDesc'));
        $dealType = trim(input('param.dealType'));
        $type = trim(input('param.type'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        $tenant = $this->db->table('tenant_info')->where(['id' => $bus['tenant_id']])->item();
        $data = array();
        $data['info']=$bus;
        $data['tenantInfo']=$tenant;
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    public function userfreezeOrThaw(){
        $id = trim(input('param.id'));
        $status=trim(input('param.status'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        $bus['status']=$status;
        $buses=$this->db->table('bus_info')->where(['id' => $id])->update($bus);
        if($buses){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
       
    }
    public function userupdateWithdrawStatus(){
        $id = trim(input('param.id'));
        $withdrawStatus=trim(input('param.withdraw_status'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_info')->where(['id' => $id])->item();
        $bus['withdraw_status']=$withdrawStatus;
        $buses=$this->db->table('bus_info')->where(['id' => $id])->update($bus);
         if($buses){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $bus]));
         }
       
    }

    public function useraftersalelist()
    {
        // ->where(['store_id' => $storeId])->lists()
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $status  = trim(input('param.status'));
        $tenantid=trim(input('param.tenantid'));
        $busId  = trim(input('param.bus_id'));
        $where=array(
            "bus_id" => $busId,
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("bus_after_sale")->order("create_time desc")->where($where)->where(['status' => $status])->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_after_sale')->where($where)->where(['status' => $status])->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    
    public function userupdateStatus()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $remark = trim(input('param.remark'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_after_sale')->where(['id' => $id])->item();
        $bus['remark']=$remark;
        $bus['status']=1;
        $buses=$this->db->table('bus_after_sale')->where(['id' => $id])->update($bus);
         if($buses){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
         }
    }

    public function userbottomsheetlist(){
        $busId  = trim(input('param.busId'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $status  = trim(input('param.status'));
        $tenantid=trim(input('param.tenantid'));
        $where=array(
            "bus_id" => $busId,
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("bus_bottom_sheet")->order("create_time desc")->where($where)->where(['status' => $status])->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_bottom_sheet')->where($where)->where(['status' => $status])->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function userbottomsheetlistupdate()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $bus = $this->db->table('bus_bottom_sheet')->where(['id' => $id])->item();
        $bus['status']=1;
        $buses=$this->db->table('bus_bottom_sheet')->where(['id' => $id])->update($bus);
         if($buses){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
         }
    }

    public function userflowList()
    {
        $tenantid=trim(input('param.tenantid'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $type  = trim(input('param.type'));
        $busId  = trim(input('param.bus_id'));
        $tol = (intval($page) - 1) * $limit;
        $where=array(
            "bus_id" => $busId,
            "type" => $type
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $list = DB::name("bus_flow")->order("create_time desc")->where($where)->where(['tenant_id' =>  $tenantid])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('bus_flow')->where($where)->where(['tenant_id' =>  $tenantid])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function useradviceList()
    {
        $status  = trim(input('param.status'));
        $suggestType  = trim(input('param.suggestType'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        if($suggestType==""){
            $list = DB::name("sys_advice")->order("create_time desc")->where(['status' => $status])->limit($tol, $limit)->select();
            $this->db = new SysDb();
            $count = $this->db->table('sys_advice')->where(['status' => $status])->count();
            $data = array();
            array_push($data, $list);
            array_push($data, $count);
        }
        else{
        $list = DB::name("sys_advice")->order("create_time desc")->where(['status' => $status])->where(['suggest_type' => $suggestType])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('sys_advice')->where(['status' => $status])->where(['suggest_type' => $suggestType])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        }
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }

    public function userely()
    {
        // ->where(['store_id' => $storeId])->lists()
        $id = trim(input('param.id'));
        $content=trim(input('param.content'));
        $this->db = new SysDb();
        $bus = $this->db->table('sys_advice')->where(['id' => $id])->item();
        $bus['status']=1;
        $bus['content']=$content;
        $buses=$this->db->table('sys_advice')->where(['id' => $id])->update($bus);
         if($buses){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "修改成功"]));
         }
    }

}
